<?php
namespace app\tripapi\controller;
use app\tripapi\model\TravelPlan;
use app\tripapi\model\TravelChat;
use app\tripapi\model\TravelChatProduct;
use think\Controller;
use think\Request;
class Negotiation extends ApiBaseController
{
    private $TravelChat;
    private $TravelChatProduct;
    private $TravelPlan;

    public function __construct()
    {
        parent::__construct();
        $this->departmentid = $_SESSION['LOGIN_DEPT_ID'];
        $this->uid = $_SESSION['LOGIN_UID']; // 填报人id
        $this->uname = str_replace(',', '', iconv('gbk', 'UTF-8', GetUserNameByUid($this->uid))); // 填报人姓名
        $this->TravelChat = new TravelChat();
        $this->TravelChatProduct = new TravelChatProduct();
        $this->TravelPlan = new TravelPlan();
    }

    /*
     * 首页
     */
    public function index()
    {
        return $this->fetch();
    }

    /*
     *  Ajax 获取洽谈单列表
     */
    public function GetList(){
//        print_r($this->params);
        if($this->params['nid'] == 'null'){
            $nid = '';
        }else{
            $nid = $this->params['nid'];
        }
        $uid = $this->uid;
        $data['page'] = $this->params['page'];
        $data['limit'] = $this->params['limit'];
        $result = $this->TravelChat->GetList($uid, $data, $nid);
        if(isset($result['0'])){
            $count = $result['0']['count'];
        }else{
            $count = '0';
        }
        $retInfo = array("status" => 200, "msg" => '获取成功',"count" => $count, "data" => $result);
        return $retInfo;
    }

    /*
     * 销售人员客户洽谈纪要添加
     */
    public function travelsaler()
    {
        $travel_num_list = $this->TravelPlan->where('user_id', '=', $_SESSION['LOGIN_UID'])->select();
        $this->assign('travel_num_list', $travel_num_list);
        return $this->fetch();
    }

    /*
    * 销售人员客户洽谈纪要编辑
    */
    public function travelsaleredit()
    {
        $travel_num_list = $this->TravelPlan->where('user_id', '=', $_SESSION['LOGIN_UID'])->select();
        $this->assign('travel_num_list', $travel_num_list);
        return $this->fetch();
    }

    /*
   * 销售人员客户洽谈纪要查看
   */
    public function travelsalershow()
    {
        $id = $_GET['id'];
        $result = $this->TravelChat->travelsalershow($id);
        $this->assign('detail', $result);
        return $this->fetch();
    }

    /*
     * 非销售人员客户洽谈纪要
     */
    public function travelnotsaler()
    {
        $travel_num_list = $this->TravelPlan->where('user_id', '=', $_SESSION['LOGIN_UID'])->select();
        $this->assign('travel_num_list', $travel_num_list);
        return $this->fetch();
    }

    /*
     * 非销售人员客户洽谈纪要
     */
    public function travelnotsaleredit()
    {
        $id = $_GET['id'];
        $result = $this->TravelChat->travelnotsaleredit($id);
        $this->assign('detail', $result);
        return $this->fetch();
    }

    /*
     * 非销售人员客户洽谈纪要
     */
    public function travelnotsalershow()
    {
        $id = $_GET['id'];
        $result = $this->TravelChat->travelnotsaleredit($id);
//        print_r($result);exit;
        $this->assign('detail', $result);
        return $this->fetch();
    }

    /*
     * 销售保存
     */
    public function TravelChatSave()
    {
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $id = $this->TravelChat->TravelChatSave($this->params);
        if ($id['id']) {
            $this->TravelChatProduct->TravelChatProductSave($id['id'], $this->params);
            $retInfo = array("status" => 200, "msg" => '申请成功', "data" => $id);
        } else {
            $retInfo = array("status" => 500, "msg" => '申请失败', "data" => array());
        }
        return json_api($retInfo);
    }

    /*
     * 销售更新
     */
//    public function TravelChatUpdate(){
//        $this->params['creator'] = $this->uname; // 创建人
//        $this->params['creator_id'] = $this->uid; // 创建人id
//    }
    /*
     * 删除
     */
    public function ChatDelete(){
        $id = $_GET['id'];
        $result = $this->TravelChat->ChatDelete($id);
        if($result){
            $this->TravelChatProduct->ChatProductDelete($id);
            $retInfo = array("status" => 200, "msg" => '删除成功', "data" => array());
        }else{
            $retInfo = array("status" => 500, "msg" => '删除失败', "data" => array());
        }
        return json_api($retInfo);
    }

    /*
     * 非销售保存
     */
    public function TravelNotSalerChatSave()
    {
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $id['id'] = $this->TravelChat->TravelNotSalerChatSave($this->params);
        if ($id['id']) {
            $retInfo = array("status" => 200, "msg" => '申请成功', "data" => $id);
        } else {
            $retInfo = array("status" => 500, "msg" => '申请失败', "data" => array());
        }
        return json_api($retInfo);
    }

    /*
     * 审批
     */
    public function ApproveTo(){
        $uid = $this->uid;
        $id = $this->params['nid'];
        # 根据id 查询 当前申请单的创建人  创建人部门
        $create_user = $this->TravelChat->GetCreateUserByUid($id);
        $user_id = $create_user['uid'];
        $uname = $create_user['user_name'];
        $department = $create_user['department_name'];
        # 用申请单的创建人 查询审批流 依次判定
        $approve = $this->TravelChat->GetApproveStatus($id,$uid, $user_id, $uname, $department);
        $retInfo = array("status" => 200, "msg" => '审批成功', "data" => $approve);
        return json_api($retInfo);
    }

    /*
     *  总经办查看洽谈单
     */
    public function managersalershow(){
        return $this->fetch();
    }

    public function ManagerGetList(){
        $result = $this->TravelChat->GetSalerList();
        $retInfo = array("status" => 200, "msg" => '123213', "data" => $result);
        return json_api($retInfo);
    }

    public function managetravelsalershow(){
        $id = $_GET['id'];
        $result = $this->TravelChat->Managetravelsalershow($id);
        $product = $this->TravelChatProduct->GetTravelChatProduct($id);
        foreach($product as $key => $value){
            $product[$key]['product_name'] = iconv('GBK', 'UTF-8', $value['product_name']);
            $product[$key]['demand'] = iconv('GBK', 'UTF-8', $value['demand']);
        }
        $this->assign('detail', $result);
        $this->assign('products', $product);
        return $this->fetch();
    }

    public function managetravelnotsalershow(){
        $id = $_GET['id'];
        $result = $this->TravelChat->travelnotsaleredit($id);
        $this->assign('detail', $result);
        return $this->fetch();
    }
}